/**
 * 节点 + 指针
 */
class Node {
  constructor(value, next = null) {
    this.val = value;
    this.next = next;
  }
}

function ListNode() {
  let head = new Node(1);
  head.next = new Node(2);
  head.next.next = new Node(3);
  head.next.next.next = new Node(4);

  let p = head, ret = '';
  while(p) {
    ret += ` ${p.val} ->`;
    p = p.next;
  }
  ret += 'null';
  console.log(ret);
}
// ListNode();

/**
 * 数组
 */
function listNode2() {
  const data = [];
  const next = [];

  function addNode(ind, p, val) {
    next[p] = next[ind];
    next[ind] = p;
    data[p] = val;
  }

  let head = 3;
  data[3] = 'a';

  addNode(3, 5, 'b');
  addNode(5, 7, 'c');
  addNode(7, 2, 'd');
  addNode(2, 1, 'e');
  addNode(7, 4, 'f');

  let p = head, ret = '';
  while(p) {
    ret += ` ${data[p]} ->` 
    p = next[p];
  }
  ret += 'null';
  console.log(ret);
}
listNode2();